﻿using System;
using System.Configuration;
using System.Reflection;

namespace VDataAdapter.Util
{
    public  class Setting
    {
        public Setting()
        {
            Reload();
        }

        public int SessionTimeOut { get; set; }


        public string ChartURI { get; set; }

        public string PriceURI { get; set; }

        public TimeSpan TimeStart { get; set; }

        public TimeSpan TimeEnd { get; set; }

        public string TcpServerIP { get; set; }
        public int TcpServerPort { get; set; }
       
        public void Reload()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            PropertyInfo[] propertyInfos = GetType().GetProperties();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.PropertyType == typeof(TimeSpan)) //for TimeStart & TimeEnd
                {
                    object obj = TimeSpan.Parse(config.AppSettings.Settings[propertyInfo.Name].Value);
                    propertyInfo.SetValue(this, obj, null);
                }
                else
                {
                    object obj = Convert.ChangeType(config.AppSettings.Settings[propertyInfo.Name].Value,
                                                propertyInfo.PropertyType);
                    propertyInfo.SetValue(this, obj, null);
                }
            }
        }
    }
}